// 通知
const { getAll, add, getCount, getOne, remove, update } = require('../utils/dbTools');
const tableName = 'notice';

/**
 * 获取通知总数
 * @param {object} map 查询条件
*/

const getNoticeTotal = async (map) => {
    const result = await getCount(tableName, map);
    return result;
}

/**
 * 获取通知列表
 * @param {object} map 查询条件
*/

const getNoticeList = async (map) => {
    const result = await getAll(tableName, map);
    return result;
}

/**
 * 新增通知方法
 * @param {object} options 通知参数
 */
const noticeAdd = async (options) => {
    const { uid, title, content, option_type, origin_id } = options;
    if (!uid || !title || !content || !option_type || !origin_id) {
        return false;
    }
    const noticeData = {
        uid, title, content, option_type, origin_id
    }
    const result = await add(noticeData, tableName);
    if (!result) {
        return false
    }
    return true;
}

/**
 * 通知详情 
 * @param {number} id 通知id
 */

const getNoticeInfo = async (id) => {
    if (!id) {
        return false
    }
    const result = await getOne(tableName, { id })
    if (!result) {
        return false;
    }
    return result;
}

/**
 * 删除通知 
 * @param {number} id 通知id
 */

const removeNotice = async (id) => {
    if (!id) return false
    const result = await remove(id, tableName)
    return result;
}


/**
 *  批量更新通知状态 
 *  @param {number|object} map 更新条件, 可以传递id或者一个对象作为更新条件
 *  @parem {object} fields 要更新的字段
 */
const updateNotice = async (map, fields) => {
    if (!map || !fields) return false
    const result = await update(map, fields, tableName)
    return result;
}

module.exports = {
    noticeAdd,
    getNoticeList,
    getNoticeTotal,
    getNoticeInfo,
    removeNotice,
    updateNotice
}